package vip.denglei.company.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Objects;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.denglei.company.mapper.AppUserAddressMapper;
import vip.denglei.company.model.app.AppUserAddress;
import vip.denglei.company.service.app.AppUserAddressService;
import vip.denglei.company.util.HttpResult;

import javax.annotation.Resource;

@Service
public class AppUserAddressServiceImpl extends ServiceImpl<AppUserAddressMapper, AppUserAddress> implements AppUserAddressService {
    @Resource
    private AppUserAddressMapper appUserAddressMapper;


    @Override
    @Transactional
    public HttpResult saveAppUserAddress(AppUserAddress appUserAddress) {
        //新增为默认收货地址,修改该用户下所有的地址为非默认地址
        if (Objects.equal(appUserAddress.getDefaultFlag(),1)) {
            LambdaUpdateWrapper<AppUserAddress> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(AppUserAddress::getDefaultFlag, 0);
            lambdaUpdateWrapper.eq(AppUserAddress::getUserId, appUserAddress.getUserId());
            this.update(lambdaUpdateWrapper);
        }
        this.saveOrUpdate(appUserAddress);
        return HttpResult.success(appUserAddress);
    }
}
